﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Actor/CreateEnemyTrigger.h"

#include "Character/PlayerCharacter.h"
#include "GameMode/CombatGameMode.h"
#include "Kismet/GameplayStatics.h"


// Sets default values
ACreateEnemyTrigger::ACreateEnemyTrigger()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	BoxComponent = CreateDefaultSubobject<UBoxComponent>("BoxComponent");
	BoxComponent->SetupAttachment(RootComponent);
}

// Called when the game starts or when spawned
void ACreateEnemyTrigger::BeginPlay()
{
	Super::BeginPlay();
	BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &ACreateEnemyTrigger::OnComponentBeginOverlap);
}

// Called every frame
void ACreateEnemyTrigger::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void ACreateEnemyTrigger::OnComponentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
                                                  UPrimitiveComponent* OtherComp,
                                                  int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	APlayerCharacter* Player = Cast<APlayerCharacter>(OtherActor);
	if (Player && !HaveCreateEnemy)
	{
		ACombatGameMode* GameMode = Cast<ACombatGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
		if (GameMode)
		{
			TArray<ACreateEnemyPoint*> EnemyPoints = GameMode->CreateEnemyPoints;
			for (ACreateEnemyPoint* point : EnemyPoints)
			{
				point->CreateEnemy();
				GameMode->EnemyNum = GameMode->EnemyNum + 1;
			}
			HaveCreateEnemy = true;
		}
	}
}
